package org.dromara.bykt.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bykt.domain.bo.BYKTCourseScoreBo;
import org.dromara.bykt.domain.vo.BYKTCourseScoreVo;
import org.dromara.bykt.domain.BYKTCourseScore;
import org.dromara.bykt.mapper.BYKTCourseScoreMapper;
import org.dromara.bykt.service.IBYKTCourseScoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 考试分数Service业务层处理
 *
 * @author Hou
 * @date 2025-06-22
 */
@RequiredArgsConstructor
@Service
public class BYKTCourseScoreServiceImpl implements IBYKTCourseScoreService {

    private final BYKTCourseScoreMapper baseMapper;

    /**
     * 查询考试分数
     *
     * @param id 主键
     * @return 考试分数
     */
    @Override
    public BYKTCourseScoreVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询考试分数列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 考试分数分页列表
     */
    @Override
    public TableDataInfo<BYKTCourseScoreVo> queryPageList(BYKTCourseScoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BYKTCourseScore> lqw = buildQueryWrapper(bo);
        Page<BYKTCourseScoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的考试分数列表
     *
     * @param bo 查询条件
     * @return 考试分数列表
     */
    @Override
    public List<BYKTCourseScoreVo> queryList(BYKTCourseScoreBo bo) {
        LambdaQueryWrapper<BYKTCourseScore> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BYKTCourseScore> buildQueryWrapper(BYKTCourseScoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BYKTCourseScore> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BYKTCourseScore::getId);
        lqw.eq(bo.getStuId() != null, BYKTCourseScore::getStuId, bo.getStuId());
        lqw.eq(bo.getCourseId() != null, BYKTCourseScore::getCourseId, bo.getCourseId());
        lqw.eq(bo.getSimulationId() != null, BYKTCourseScore::getSimulationId, bo.getSimulationId());
        lqw.eq(bo.getScore() != null, BYKTCourseScore::getScore, bo.getScore());
        lqw.eq(bo.getExamSpentTime() != null, BYKTCourseScore::getExamSpentTime, bo.getExamSpentTime());
        return lqw;
    }

    /**
     * 新增考试分数
     *
     * @param bo 考试分数
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BYKTCourseScoreBo bo) {
        BYKTCourseScore add = MapstructUtils.convert(bo, BYKTCourseScore.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改考试分数
     *
     * @param bo 考试分数
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BYKTCourseScoreBo bo) {
        BYKTCourseScore update = MapstructUtils.convert(bo, BYKTCourseScore.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BYKTCourseScore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除考试分数信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
